package com.cetc.ga.service.impl;

import com.cetc.ga.domain.repository.UserRepository;
import com.cetc.ga.service.UserService;
import com.cetc.ga.domain.entity.UserEntity;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserRepository userRepository;

    @Override
    public void insert(UserEntity userEntity) {
        userRepository.save(userEntity);
    }

    @Override
    public void insertAll(List<UserEntity> userEntityList) {
        if(null == userEntityList || userEntityList.isEmpty()) {
            logger.info("数据记录条目不能为空");
            return;
        }

        logger.info("----------开始记录----------数据条目:{}", userEntityList.size());
        // 查询重复数据
        List<UserEntity> userEntityInDb = Lists.newArrayList();
        for(UserEntity userEntity: userEntityList) {
            UserEntity ue = findById(userEntity.getId());
            //UserEntity ue = findByCardNo(userEntity.getCardNo());
            if(null != ue) {
                userEntityInDb.add(ue);
            }
        }
        // 去掉重复数据
        userEntityList.removeAll(userEntityInDb);

        //userRepository.saveAll(userEntityList);
        userRepository.save(userEntityList);
        logger.info("----------记录结束----------数据条目:{}", userEntityList.size());
    }

    @Override
    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }

    @Override
    public void deleteByCardNo(String cardNo) {
        userRepository.deleteByCardNo(cardNo);
    }

    @Override
    public void update(UserEntity userEntity) {
        userRepository.update(userEntity.getCardNo(),userEntity.getUserName(),
                userEntity.getPassWord(),userEntity.getUserSex(),userEntity.getNickName());
        //userRepository.update(userEntity);
    }

    @Override
    public void updateNoNull(UserEntity userEntity) {
        UserEntity ue = findByCardNo(userEntity.getCardNo());
        if(null == userEntity.getCardNo() || "".equals(userEntity.getCardNo())) {
            userEntity.setCardNo(ue.getCardNo());
        }
        if(null == userEntity.getUserName() || "".equals(userEntity.getCardNo())) {
            userEntity.setUserName(ue.getUserName());
        }
        if(null == userEntity.getPassWord() || "".equals(userEntity.getPassWord())) {
            userEntity.setPassWord(ue.getPassWord());
        }
        if(null == userEntity.getUserSex() || "".equals(userEntity.getUserSex())) {
            userEntity.setUserSex(ue.getUserSex());
        }
        if(null == userEntity.getNickName() || "".equals(userEntity.getNickName())) {
            userEntity.setNickName(ue.getNickName());
        }

        userRepository.update(userEntity.getCardNo(),userEntity.getUserName(),
                userEntity.getPassWord(),userEntity.getUserSex(),userEntity.getNickName());
    }

    @Override
    public List<UserEntity> findAll() {
        return userRepository.findAll();
    }


    @Override
    public UserEntity findById(Long id) {
        /*
        Optional<UserEntity> findResult = userRepository.findById(id);
        return findResult.get();
        */
        return userRepository.findOne(id);
    }

    @Override
    public UserEntity findByCardNo(String cardNo) {
        return userRepository.findOne(cardNo);
    }

    @Override
    public List<UserEntity> findByName(String userName) {
        return userRepository.findByName(userName);
    }
}
